﻿using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace PropertyManagementSystemAPI.Authodtion
{
    public class TokenAuthenticationService : IAuthenticateService
    {
        private readonly TokenManagement _tokenManagement;
        public TokenAuthenticationService(IOptions<TokenManagement> tokenManagement)
        {

            _tokenManagement = tokenManagement.Value;
        }
        public bool IsAuthenticated(Models.Systeminfo request, out string token)
        {
            token = string.Empty;

            var claims = new[]
            {
             new Claim(ClaimTypes.Name,request.Sysname),
               new Claim("password",request.Syspassword),
                 new Claim("name",request.Sysname)
         };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenManagement.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(_tokenManagement.Issuer, _tokenManagement.Audience, claims, expires: DateTime.Now.AddMinutes(_tokenManagement.AccessExpiration), signingCredentials: credentials);

            token = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return true;
        }

        public bool IsAuthenticated(Models.Ownerinfo request, out string token)
        {
            token = string.Empty;

            var claims = new[]
            {
             new Claim(ClaimTypes.Name,request.Ologincode),
               new Claim("password",request.Password),
                 new Claim("name",request.Orealname)
         };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenManagement.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(_tokenManagement.Issuer, _tokenManagement.Audience, claims, expires: DateTime.Now.AddMinutes(_tokenManagement.AccessExpiration), signingCredentials: credentials);

            token = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return true;
        }
    }
}
